﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Sims3Package;
using Sims3Package.Interface;
using Sims3Package.Wrapper;

namespace PackageEditor
{
    public partial class FilterPanel : UserControl
    {
        private ListView listview = null;

        public FilterPanel()
        {
            InitializeComponent();
        }

        public FilterPanel(ref ListView listview)
        {
            InitializeComponent();
            this.listview = listview;

            // hook the event
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(TextBox))
                    c.TextChanged += new EventHandler(OnInfoEntered);
            }
            
        }

        
        private void OnInfoEntered(object sender, EventArgs e)
        {
            bool test = true;
            if (ckbGroup.Checked) test = test ? ResourceForm.UInt32Parse(txtGroup.Text) : false;
            if (ckbType.Checked) test = test ? ResourceForm.UInt32Parse(txtType.Text) : false;
            if (ckbInstance.Checked) test = test ? ResourceForm.UInt64Parse(txtType.Text) : false;
            this.btnFilter.Enabled = test;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            listview.Items.Clear();
            lblWait.Text = "Working, please wait";
            bgwRead.RunWorkerAsync();
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private bool Test(ResourceEntry entry)
        {
            
            if (ckbType.Checked && entry.ResourceType != Convert.ToUInt32(txtType.Text, 16)) return false;
            if (ckbGroup.Checked && entry.ResourceGroup!= Convert.ToUInt32(txtGroup.Text, 16)) return false;
            if (ckbInstance.Checked && entry.ResourceInstance != Convert.ToUInt64(txtInstance.Text, 16)) return false;
            return true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listview.Items.Clear();

            foreach(Control c in this.Controls)
                if(c.GetType() == typeof(CheckBox)) ((CheckBox)c).Checked = false;
            lblWait.Text = "Working, please wait";
            bgwRead.RunWorkerAsync();
        }

        private void bgwRead_DoWork(object sender, DoWorkEventArgs e)
        {
            string Itemname = txtName.Text.ToUpper();
            string Tagname = txtTag.Text.ToUpper();
            string type = txtTag.Text;
            string group = txtGroup.Text;
            string instance = txtInstance.Text;

            if (MainForm.pack == null) return;
            Dictionary<UInt64, string> nameMap = new Dictionary<ulong, string>();
            List<ListViewItemEx> items = new List<ListViewItemEx>();
            if (MainForm.pack.EntryList.Exists(entry => entry.ResourceType == 0x0166038CU))
                foreach (IResourceEntry entry in MainForm.pack.EntryList.FindAll(entry => entry.ResourceType == 0x0166038CU))
                {
                    NameMapResourceWrapper w = new NameMapResourceWrapper(MainForm.pack.GetResourceStream(entry));
                    foreach (var kvp in w.NameMap) nameMap.Add(kvp.Key, kvp.Value);
                }
            foreach (ResourceEntry entry in MainForm.pack.EntryList.Where(item =>
            {
                if (ckbType.Checked && item.ResourceType != Convert.ToUInt32(type, 16)) return false;
            if (ckbGroup.Checked && item.ResourceGroup != Convert.ToUInt32(group, 16)) return false;
            if (ckbInstance.Checked && item.ResourceInstance != Convert.ToUInt64(instance, 16)) return false; return true;
            }))
            {
                string name = nameMap.Keys.Contains(entry.ResourceInstance) ? nameMap[entry.ResourceInstance] : string.Empty;
                ListViewItemEx item = new ListViewItemEx(entry, name);
                item.SubItems.AddRange(new string[]{MainForm.TagMap.ContainsKey(item.ResourceType) ? MainForm.TagMap[item.ResourceType] :string.Empty, string.Format("0x{0:X8}", item.ResourceType)
                , string.Format("0x{0:X8}", item.ResourceGroup)
                , string.Format("0x{0:X16}", item.ResourceInstance)});
                if (ckbName.Checked || ckbTag.Checked)
                {
                    bool test = true;
                    test = ckbName.Checked ? item.Text.ToUpper().Contains(Itemname) : test;
                    test = ckbTag.Checked ? item.SubItems[1].ToString().ToUpper().Contains(Tagname) : test;
                    if (test) items.Add(item);
                }
                else
                {
                    items.Add(item);
                }
            }

            e.Result = items;
        }

        private void bgwRead_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblWait.Text = string.Empty;
            List<ListViewItemEx> items = e.Result as List<ListViewItemEx>;
            if (items == null) return;
            listview.BeginUpdate();
            foreach (var entry in items) listview.Items.Add(entry);
            listview.EndUpdate();
            items = null;
            
        }
    }
}
